package my.edu.common.constant;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Locale;

public class OrderConstant {
    public static final String NOT_PAYMENT = "未付款";
    public static final String GET_PAYMENT = "已付款";
    public static final String PART_PAYMENT = "部分付款";
    public static final String NOT_REPAYMENT = "未退款";
    public static final String GET_REPAYMENT = "已退款";
    public static final String PART_REPAYMENT = "部分退款";
    public static final String PART_RETURN = "部分退货";
    public static final String NOT_RETURN = "未退货";
    public static final String GET_RETURN = "已退货";
    public static final String ISAUDITED = "已审核";
    public static final String NOTAUDITED = "未审核";
    public static final String ISACCOUNTED = "已对账";
    public static final String NOTACCOUNTED = "未对账";
    public static final String IGNORE_PRICE = "ignore";






    //volatile：
    //一、保证可见性：
    //当一个线程修改了 volatile 变量的值，其他线程可以立即看到修改后的值。
    //非 volatile 变量可能会被线程缓存起来，导致其他线程看到的值是旧的。
    //二、禁止指令重排序：
    //volatile 变量的读写操作不能与其他指令进行重排序，确保代码的执行顺序符合预期。
    private static volatile String cachedTimeFormat = "";
    private static volatile long lastUpdated = 0;

    //不加此代码段，则app有自己的时间，不和系统时间一致
    static {
        // 获取当前时间的 LocalDateTime
        LocalDateTime now = LocalDateTime.now();

        // 将当前时间设置为当前分钟的第一秒
        LocalDateTime startOfMinute = now.truncatedTo(ChronoUnit.MINUTES);

        // 转换为时间戳
        ZonedDateTime zonedDateTime = startOfMinute.atZone(ZoneId.systemDefault());

        // 转换为本地时区的时间戳
        lastUpdated = zonedDateTime.toInstant().toEpochMilli();
        cachedTimeFormat =now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
        // 输出验证
        System.out.println("lastUpdated: " + lastUpdated);
    }

    //加入缓存
    public static String getCurrentTimeFormat() {
        long currentMillis = System.currentTimeMillis();
        if (currentMillis - lastUpdated > 60000) { // 超过一分钟
            synchronized (OrderConstant.class) {
                if (currentMillis - lastUpdated > 60000) {
                    cachedTimeFormat = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
                    lastUpdated = currentMillis;
                }
            }
        }
        return cachedTimeFormat;
    }

}
